package apriorialgo;

import java.io.IOException;
import java.util.Iterator;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class C1ItemsetReducer extends
		Reducer<Text, IntWritable, Text, IntWritable> {
	
	private double minSup = -1.0;
	private int noTransaction = -1;
	
	@Override
	protected void setup(Context context) throws IOException,
			InterruptedException {
		super.setup(context);
		System.out.println("Enter C1ItemsetReducer.setup");
		noTransaction = context.getConfiguration().getInt("no.trans", -1);
		System.out.println("C1ItemsetReducer.noTransaction=" + noTransaction);		
		String minSupport = context.getConfiguration().get("min.support", "-1.0");		
		System.out.println("C1ItemsetReducer.minSup=" + minSupport);
		minSup = Double.valueOf(minSupport).doubleValue();		
	}

	@Override
	public void reduce(Text key, Iterable<IntWritable> values, Context context)
			throws IOException {
		int sum = 0;
		Iterator<IntWritable> iterator = values.iterator();
		while (iterator.hasNext()) {
			sum += iterator.next().get();
		}
		if ((sum / (double) (noTransaction)) >= minSup) {
			try {
				context.write(key, new IntWritable(sum));
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}		
	}
}
